package {{.ModuleName}}

import (
	"dims/entity"
	"fmt"
)


//不分页查询list
func (d *DataSource) {{.ClassName}}SelectList(req *entity.{{.ClassName}}PageReq) (list []*entity.{{.ClassName}}, err error) {

    tx := d.DB.Table("{{.Table_Name}}")
    var beginTime = req.BeginTime
    var endTime = req.EndTime

    // 构建 WHERE 子句
    whereClauses := []string{}
    values := []interface{}{}
    {{- range .Fields}}
    {{- if eq .IsQuery "1" }}
    if req.{{upper (.JavaField)}} != ""  {
        {{- if eq .QueryType "EQ" }}
        whereClauses = append(whereClauses, "{{.ColumnName}} = ?")
        values = append(values, req.{{upper (.JavaField)}})
        {{- else if eq .QueryType "LIKE" }}
        whereClauses = append(whereClauses, "{{.ColumnName}} LIKE ?")
        values = append(values, "%" + req.{{upper (.JavaField)}} + "%")
        {{- end }}
    }
    {{- end }}
    {{- end }}

    if beginTime != "" {
         whereClauses = append(whereClauses, "create_time >= ?")
         beginTimeStr := utils.GetBeginTime(beginTime)
         values = append(values, beginTimeStr)
    }

    if endTime != "" {
        whereClauses = append(whereClauses, "create_time <= ?")
        endTimeStr := utils.GetBeginTime(endTime)
        values = append(values, endTimeStr)
    }


    if len(whereClauses) > 0 {
        tx = tx.Where(strings.Join(whereClauses, " AND "), values...)
    }

    if len(whereClauses) > 0 {
        tx = tx.Where(strings.Join(whereClauses, " AND "), values...)
    }
    err = tx.Find(&list).Error
	if err != nil {
		return nil, err
	}
	return
}


//按条件分页查询 {{.ClassName}}Page 分页查询{{.ClassName}}信息
func (d *DataSource) {{.ClassName}}QueryPage(req *entity.{{.ClassName}}PageReq) (list []*entity.{{.ClassName}}, count int64, err error)  {
    tx := d.DB.Table("{{.Table_Name}}")
    var beginTime = req.BeginTime
    var endTime = req.EndTime

	// 可以自定义搜索方式

    // 构建 WHERE 子句
    whereClauses := []string{}
    values := []interface{}{}
    {{- range .Fields}}
    {{- if eq .IsQuery "1" }}
    if req.{{upper (.JavaField)}} != ""  {
        {{- if eq .QueryType "EQ" }}
        whereClauses = append(whereClauses, "{{.ColumnName}} = ?")
        values = append(values, req.{{upper (.JavaField)}})
        {{- else if eq .QueryType "LIKE" }}
        whereClauses = append(whereClauses, "{{.ColumnName}} LIKE ?")
        values = append(values, "%" + req.{{upper (.JavaField)}} + "%")
        {{- end }}
    }
    {{- end }}
    {{- end }}

    if beginTime != "" {
         whereClauses = append(whereClauses, "create_time >= ?")
         beginTimeStr := utils.GetBeginTime(beginTime)
         values = append(values, beginTimeStr)
    }

    if endTime != "" {
        whereClauses = append(whereClauses, "create_time <= ?")
        endTimeStr := utils.GetBeginTime(endTime)
        values = append(values, endTimeStr)
    }

    if len(whereClauses) > 0 {
        tx = tx.Where(strings.Join(whereClauses, " AND "), values...)
    }

    tx.Count(&count)
    tx = buildPageOrderCtx(req.Pager, tx)
    tx.Limit(req.Size).Offset((req.Current - 1) * req.Size).Find(&list)
    return
}